package com.iflysse.controller;

import com.iflysse.common.ActivityState;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.TaskBoReq;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.TaskVoReq;
import com.iflysse.services.TaskService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("/task")
@Api(value = "任务模块接口")
@Log4j
public class TaskController {



    @Autowired
    private TaskService taskService;

    /**
     * 教师端添加学习任务
     */
    @PostMapping("/addTask")
    @ApiOperation(value = "教师端添加学习任务")
    public CommonVoResp doAddTask(TaskVoReq taskVoReq){

        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("教师端添加学习任务参数bean:"+taskVoReq);

            TaskBoReq taskBoReq=CommonUtil.objToObj(taskVoReq,TaskBoReq.class);

            taskBoReq.setTaskExpirationDate(CommonUtil.stringSourceToDate(taskVoReq.getTaskExpirationDate(),CommonUtil.pattern));
            taskBoReq.setChapterIdList(Arrays.asList(taskVoReq.getChapterIdList().split(",")));
            if(taskVoReq.getState().equals(ActivityState.STATE_NO_PUBLISH.getStateId())){
                taskBoReq.setState(ActivityState.STATE_NO_PUBLISH);
            }else if(taskVoReq.getState().equals(ActivityState.STATE_FINISH.getStateId())){
                taskBoReq.setState(ActivityState.STATE_FINISH);
            }else if(taskVoReq.getState().equals(ActivityState.STATE_RUNNING.getStateId())){
                taskBoReq.setState(ActivityState.STATE_RUNNING);
            }

            CommonBoResp commonBoResp=taskService.publishTask(taskBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("教师端添加学习任务响应bean:"+commonVoResp);
            return commonVoResp;

        }catch (Exception e){
            log.error("教师端添加学习任务出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }

    }




}
